package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IPermissionService;

import cn.wolfcode.util.UserContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginControcller {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IPermissionService permissionService;


    @RequestMapping("/login")
    @ResponseBody
    public JsonResult login(String username, String password, HttpSession session) {

            Employee employee = employeeService.login(username, password);
            //把查询出的员工对象放到session中
            //session.setAttribute("EMPLOYEE_IN_SESSION", employee);
            //优化提出工具类
            UserContext.setCurrentEmployee(employee);
            if (!employee.isAdmin()) {//非管理员的员工才需要缓存权限数据
                //登录成功之后把此员工对应的权限数据存到session缓存起来
                List<String> expressions =
                        permissionService.queryByEmployeeId(employee.getId());
                //session.setAttribute("EXPRESSIONS_IN_SESSION",expressions);
                UserContext.setExpressions(expressions);

            }
            return new JsonResult(true, "登录成功");



    }

}
